import { DeviceDTO } from '@/api/entity/device'
import { getDeviceListBySceneIdAPI } from '@/api/device'

export function clearDataset () {
  SceneDataset.clearData()
  DeviceDataset.clearData()
}

// region TODO 场景
let sceneId = ''
export const SceneDataset = {
  setSceneId (id: string) {
    sceneId = id
  },
  clearData () {
    sceneId = ''
  }
}
// endregion

// region TODO 设备列表

let deviceList: DeviceDTO[] = null
export const DeviceDataset = {
  getDeviceList () {
    return new Promise<DeviceDTO[]>((resolve) => {
      if (deviceList) {
        resolve(deviceList)
        return
      }
      getDeviceListBySceneIdAPI({ id: sceneId }).then((res) => {
        res.result().then((rdata) => {
          deviceList = rdata.data || []
          resolve(deviceList)
        }).catch(() => resolve([]))
      }).catch(() => resolve([]))
    })
  },
  clearData () {
    sceneId = ''
    deviceList = null
  },
  reloadData () {
    deviceList = null
    return this.getDeviceList()
  }
}
// endregion
